package tingche.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import tingche.bean.ShouFeiBean;
import util.JDBCUtils;

public class ShouFeiDao {
	Connection con=null;
	PreparedStatement ps=null;
	public int insert(ShouFeiBean shoufei) {
		String sql="insert into c_shoufei(cheweihao,chepai,starttime) values(?,?,?)";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setInt(1, shoufei.getCheWeiHao());
			ps.setString(2, shoufei.getChePai());
			ps.setString(3, shoufei.getStartTime());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	public ShouFeiBean selectByPaiTing(ShouFeiBean sf) {
		String sql="select * from c_shoufei where cheweihao=? and chepai=?";
		ResultSet rs=null;
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setInt(1, sf.getCheWeiHao());
			ps.setString(2, sf.getChePai());
			rs=ps.executeQuery();
			rs.next();
			ShouFeiBean sfbean=new ShouFeiBean();
			sfbean.setChePai(rs.getString("chepai"));
			sfbean.setCheWeiHao(rs.getInt("cheweihao"));
			sfbean.setStartTime(rs.getString("starttime"));
			sfbean.setEndTime(rs.getString("endtime"));
			sfbean.setMoney(rs.getBigDecimal("money"));
			return sfbean;
		} catch (SQLException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public int update(ShouFeiBean sf) {
		String sql="update c_shoufei set endtime=?,money=? where cheweihao=? and chepai=?";
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			ps.setString(1, sf.getEndTime());
			ps.setBigDecimal(2, sf.getMoney());
			ps.setInt(3, sf.getCheWeiHao());
			ps.setString(4, sf.getChePai());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	public List<ShouFeiBean> selectAll(){
		String sql="select * from c_shoufei ";
		ResultSet rs=null;
		List<ShouFeiBean> list=new ArrayList<>();
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				ShouFeiBean bean=new ShouFeiBean();
				bean.setChePai(rs.getString("chepai"));
				bean.setCheWeiHao(rs.getInt("cheweihao"));
				bean.setEndTime(rs.getString("endtime"));
				bean.setMoney(rs.getBigDecimal("money"));
				bean.setStartTime(rs.getString("starttime"));
				list.add(bean);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
